#include "Magnet.h"
#include "GameObject.h"
#include "..\SGD Wrappers\CSGD_TextureManager.h"
#include "..\SGD Wrappers\CSGD_Direct3D.h"
#include "MagObject.h"
#include "GamePlayState.h"
#include "ObjectManager.h"
#include "Camera.h"
#include "Physics.h"
#include "Arm.h"

// Constructor
Magnet::Magnet(void)
{
	ImageLR = CSGD_TextureManager::GetInstance()->LoadTexture("Textures\\Objects\\MagnetTest.png");
	ImageUD = CSGD_TextureManager::GetInstance()->LoadTexture("Textures\\Objects\\MagnetUDTest.png");
	attachedObject = nullptr;
	magObj = new MagObject(this);

	ObjectManager::GetInstance()->AddObject(4, magObj);
}

// Destructor
Magnet::~Magnet(void)
{
}

void Magnet::AttachObject(GameObject* obj)
{
	if(attachedObject == nullptr)
		attachedObject = obj;
	RECT none = {0,0,0,0};

	magObj->SetHitBox(none);
}

void Magnet::ReleaseObject(void)
{
	////if player is facing up, translate the attached object down
	//if(GamePlayState::GetInstance()->GetPlayer()->GetHeading().fY == 1)
	//{
	//	attachedObject->SetPosY( attachedObject->GetPosY() + (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top));
	//}
	attachedObject = nullptr;
}

void Magnet::Update(float deltaTime)
{
	////Magnet is active
	if(isActive)
	{
		Player* player = GamePlayState::GetInstance()->GetPlayer();
		float tempwidth = (float)CSGD_TextureManager::GetInstance()->GetTextureWidth(ImageLR)/4;
		float tempheight = (float)CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageUD)/4;

		if (attachedObject == nullptr)
		{
			if(player->GetHeading().fY == 0 && player->GetHeading().fX == -1)
			{
				hitbox.top = (long)arm->GetToolPointY();
				hitbox.bottom = (long)arm->GetToolPointY() + CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR)/4;
				hitbox.top -= CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR)/8;
				hitbox.bottom -= CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR)/8;
				hitbox.left = (long)arm->GetToolPointX();
				hitbox.right = hitbox.left + (long)tempwidth;
			}

			else if(player->GetHeading().fY == 0 && player->GetHeading().fX == 1)
			{
				hitbox.top = (long)arm->GetToolPointY();
				hitbox.bottom = (long)arm->GetToolPointY() + CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR)/4;
				hitbox.top -= CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR)/8;
				hitbox.bottom -= CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR)/8;
				hitbox.right = (long)arm->GetToolPointX();
				hitbox.left = hitbox.right - (long)tempwidth;
			}

			else if(player->GetHeading().fY == 1 )
			{
				hitbox.left =  (LONG)arm->GetToolPointX();
				hitbox.right = (LONG)(arm->GetToolPointX() + tempwidth);
				hitbox.left -= (LONG)(tempwidth/2);
				hitbox.right = (LONG)(arm->GetToolPointX() + tempwidth/2);
				hitbox.bottom =(LONG)(arm->GetToolPointY());
				hitbox.top = hitbox.bottom - (long)tempheight;
			}

			else if(player->GetHeading().fY == -1)
			{
				hitbox.left =  (LONG)arm->GetToolPointX();
				hitbox.right = (LONG)(arm->GetToolPointX() + tempwidth);
				hitbox.left -= (LONG)(tempwidth/2);
				hitbox.right = (LONG)(arm->GetToolPointX() + tempwidth/2);
				hitbox.top =   (LONG)(arm->GetToolPointY());
				hitbox.bottom = hitbox.top + (long)tempheight;
			}

			magObj->SetHitBox(hitbox);  
		}


		//If attached object update
		if(attachedObject != nullptr)
		{
			if ( attachedObject->GetObjectID() == "Metal Box" )
			{
				if((player->GetHeading().fY == 0 || player->GetHeading().fY == -1) && player->GetHeading().fX == -1)
				{
					attachedObject->SetPosX((float)arm->GetToolPointX() + 32);
					attachedObject->SetPosY((float)arm->GetToolPointY() - (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top)/2);
				}

				else if((player->GetHeading().fY == 0 || player->GetHeading().fY == -1) && player->GetHeading().fX == 1)
				{
					attachedObject->SetPosX((float)arm->GetToolPointX() - 32 - (attachedObject->GetHitBox().right - attachedObject->GetHitBox().left));
					attachedObject->SetPosY((float)arm->GetToolPointY() - (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top)/2);
				}

				else if(player->GetHeading().fY == 1 )
				{
					attachedObject->SetPosX((float)arm->GetToolPointX() - (attachedObject->GetHitBox().right - attachedObject->GetHitBox().left)/2);
					attachedObject->SetPosY((float)arm->GetToolPointY() - 32 - (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top));
				} 
			}
			else
			{
				if((player->GetHeading().fY == 0 || player->GetHeading().fY == -1) && player->GetHeading().fX == -1)
				{
					attachedObject->SetPosX((float)arm->GetToolPointX() + 32);
					attachedObject->SetPosY((float)arm->GetToolPointY() - (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top)/2);
				}

				else if((player->GetHeading().fY == 0 || player->GetHeading().fY == -1) && player->GetHeading().fX == 1)
				{
					attachedObject->SetPosX((float)arm->GetToolPointX() - 32 - (attachedObject->GetHitBox().right - attachedObject->GetHitBox().left));
					attachedObject->SetPosY((float)arm->GetToolPointY() - (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top)/2);
				}

				else if(player->GetHeading().fY == 1 )
				{
					attachedObject->SetPosX((float)arm->GetToolPointX() - (attachedObject->GetHitBox().right - attachedObject->GetHitBox().left)/2);
					attachedObject->SetPosY((float)arm->GetToolPointY() - 32 - (attachedObject->GetHitBox().bottom - attachedObject->GetHitBox().top));
				} 
			}


			RECT objcolupdate = attachedObject->GetHitBox();

			Physics::UpdateColRect(objcolupdate, (int)attachedObject->GetPosX(), (int)attachedObject->GetPosY());
			attachedObject->SetHitBox(objcolupdate);
			
		}
	}
}

void Magnet::Render( void ) const
{
	if(isActive)
	{
		RECT source = { 0,0, CSGD_TextureManager::GetInstance()->GetTextureWidth(ImageLR), CSGD_TextureManager::GetInstance()->GetTextureHeight(ImageLR) };
		Player* player = GamePlayState::GetInstance()->GetPlayer();


		if (attachedObject == nullptr)
		{
			if(player->GetHeading().fY == 0 && player->GetHeading().fX == -1)
				CSGD_TextureManager::GetInstance()->Draw(ImageLR, 
				(int)(arm->GetToolPointX() - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() - 16 - Camera::GetInstance()->OffsetY()), .125, .125);
			else if(player->GetHeading().fY == 0 && player->GetHeading().fX == 1)
				CSGD_TextureManager::GetInstance()->Draw(ImageLR, 
				(int)(arm->GetToolPointX() - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() - 16 - Camera::GetInstance()->OffsetY()), -.125, .125);
			else if(player->GetHeading().fY == 1 )
				CSGD_TextureManager::GetInstance()->Draw(ImageUD, 
				(int)(arm->GetToolPointX() - 16 - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() - 32 - Camera::GetInstance()->OffsetY()), .125, .125);
			else if(player->GetHeading().fY == -1 )
				CSGD_TextureManager::GetInstance()->Draw(ImageUD, 
				(int)(arm->GetToolPointX() - 16 - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() + 32 - Camera::GetInstance()->OffsetY()), .125, -.125);

			//RECT tempHit = hitbox;
			//tempHit.left -=		(LONG)Camera::GetInstance()->OffsetX();
			//tempHit.right -=	(LONG)Camera::GetInstance()->OffsetX();
			//tempHit.top -=		(LONG)Camera::GetInstance()->OffsetY();
			//tempHit.bottom -=	(LONG)Camera::GetInstance()->OffsetY();

			////CSGD_Direct3D::GetInstance()->DrawHollowRect(tempHit, D3DCOLOR_ARGB(255,255,0,0), 3);
		}
		else
		{
			if((player->GetHeading().fY == 0 || player->GetHeading().fY == -1) && player->GetHeading().fX == -1)
				CSGD_TextureManager::GetInstance()->Draw(ImageLR, 
				(int)(arm->GetToolPointX() - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() - 16 - Camera::GetInstance()->OffsetY()), .125, .125);
			else if((player->GetHeading().fY == 0 || player->GetHeading().fY == -1) && player->GetHeading().fX == 1)
				CSGD_TextureManager::GetInstance()->Draw(ImageLR, 
				(int)(arm->GetToolPointX() - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() - 16 - Camera::GetInstance()->OffsetY()), -.125, .125);
			else if(player->GetHeading().fY == 1 )
				CSGD_TextureManager::GetInstance()->Draw(ImageUD, 
				(int)(arm->GetToolPointX() - 16 - Camera::GetInstance()->OffsetX()), 
				(int)(arm->GetToolPointY() - 32 - Camera::GetInstance()->OffsetY()), .125, .125);
		}
	}		
}

void Magnet::DeactivateTool( void )
{
	ReleaseObject();
	isActive = false;

	RECT none = {0,0,0,0};

	magObj->SetHitBox(none);
}

